<?php

class ListController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
    	define('MYUSERNICK','浪子arvin');
    	define('PAGESIZE',20);  
    	
        $layout = $this->_helper->layout();
        $layout->title =  "送礼网";   
        $layout->keywords = "送礼送什么,礼品,送礼不愁";
        $layout->description = "送礼送什么,礼品,送礼不愁";
        
        //加载导航菜单
        $layout->tag = "list";
        $nav = new Application_Model_NavMapper();
        $layout->navlist = $nav->fetchAllSort(); 
				
		$pagesize = PAGESIZE;
		$page = $this->_request->getParam('page');
		if (empty($page)) $page = '1';
		
		$q = $this->_request->getParam('q'); 		
		if (empty($q) || ($q=='请输入商品名称...')|| ($q=='')) {$q='热卖';}
		
		/*$patterns =array_merge(array("-",'(',')','/','+','\\'),$no_words);  //替换字串
		$replacements = '';  //输出模式替换结果
		$q=str_replace($patterns, $replacements, $q);
		
		$iid =decode_url($_GET['iid']);
		$cid =decode_url($_GET['cid']);
		if($q==''&&$cid==''){$q=HOTWORD;}*/
		
		$iid = $this->_request->getParam('iid');
		if (empty($iid)) $iid = '';
		
		$cid = $this->_request->getParam('cid');
		if (empty($cid)) $cid = '';
		
		$sort = $this->_request->getParam('sort'); 
		if (empty($sort)) $sort = 'commissionNum_desc';
		$start_price = $this->_request->getParam('start_price');
		if (empty($start_price)) $start_price = '';
		$area = $this->_request->getParam('area');
		if (empty($area)) $area = '';
		$end_price= $this->_request->getParam('end_price');
		if (empty($end_price)) $end_price = '';
		$start_credit = $this->_request->getParam('start_credit');
		if (empty($start_credit)) $start_credit = '';
		$end_credit = $this->_request->getParam('end_credit');
		if (empty($end_credit)) $end_credit = '';

		//淘宝客数据查询
        //$Taoapi_Config = Taoapi_Config::Init();
		//$Taoapi_Config->setCharset('UTF-8');	
		$Taoapi = new Taoapi_Taoapi();
		
		//查询淘宝客推广商品(taobao.taobaoke.items.get)
		$Taoapi->method = 'taobao.taobaoke.items.get';
		$Taoapi->fields = 'num_iid,iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,seller_credit_score,item_location,shop_click_url';
		$Taoapi->nick = MYUSERNICK;
		$Taoapi->cid = $cid;
		$Taoapi->keyword = $q;
		$Taoapi->start_credit = $start_credit;
		$Taoapi->end_credit = $end_credit;
		$Taoapi->start_price = $start_price;
		$Taoapi->end_price = $end_price;
		$Taoapi->sort = $sort;
		$Taoapi->area = $area;
		$Taoapi->page_no = $page;
		$Taoapi->page_size = $pagesize;
		//$Taoapi->outer_code = $_SESSION['dduserid'];		
		
		//需要更多的字段可以登陆 taoapi.com 进行配置生成
		$TaobaokeData = $Taoapi->Send('get','xml')->getArrayData();
		
		
		$TaobaokeItem1 = $TaobaokeData["taobaoke_items"]["taobaoke_item"];
		$TotalResults = $TaobaokeData["total_results"]; 
		if (!$TotalResults){
			$s8_url = "http://s.taobao.com/search?q=".urlencode(iconv('UTF-8','GBK//IGNORE',$q))."&cat=$cid&ali_trackid=2:mm_"."29993940"."_0_0&search_type=item&unid=29993940";
		}
		if($TotalResults==1){
			$TaobaokeItem2[0]=$TaobaokeItem1;
		}else{
			$TaobaokeItem2=$TaobaokeItem1;
		}
		for($i = 0; $i < count($TaobaokeItem2); $i++){
			if($i<PAGESIZE){
				$TaobaokeItem[$i]=$TaobaokeItem2[$i];
			}else{
				$ListHot[$i-PAGESIZE-1]=$TaobaokeItem2[$i];
				break;
			}
		}
		//最多显示100页
		if($TotalResults>$pagesize*100)
		{
			$total = 100*$pagesize;
		}else
		{
			$total = $TotalResults;
		}
		
		$this->view->tolal = $total;
		$this->view->pagesize = $pagesize;
		$this->view->page = $page;
		
		if($start_price=='0'){$start_price='';}
		if($end_price=='999999'){$end_price='';}
		
		//获取卖家昵称字符换
		$strnicks = "";
		for($i = 0; $i < count($TaobaokeItem); $i++){
			if($i == 0){
				$strnicks = $TaobaokeItem[$i]["nick"]; 
			}else{
				$strnicks = $strnicks . "," . $TaobaokeItem[$i]["nick"]; 
			}
		}
		
		//获取卖家信息---根据卖家昵称列表，获取对应卖家的信息
		$Taoapi->method = 'taobao.users.get';
		$Taoapi->fields = 'user_id,nick,seller_credit,location,consumer_protection,type';
		$Taoapi->nicks = $strnicks;
		$TaoapiUsers = $Taoapi->Send('get','xml')->getArrayData();
		$Result_users1 = $TaoapiUsers["users"]["user"]; 
		if($TotalResults==1){
			$Result_users[0]=$Result_users1;
		}else{
			$Result_users=$Result_users1;
		}		
		
		//网站头
		//$itemcatsname=$q;
		//获取商品类目信息
		//if($itemcatsname==''){
		//	include("comm/title.php");
		//}
		
		//组装卖家信息，和之前的产品信心结合成一个数组
		for($i=0;$i<$pagesize;$i++){
			if($TaobaokeItem[$i]["nick"]!="")
			{
				$TaobaokeItem[$i]["nick_url"] = urlencode($TaobaokeItem[$i]["nick"]);
				$TaobaokeItem[$i]["user_id"]= $Result_users[$i]["user_id"];
				$TaobaokeItem[$i]["type"]= $Result_users[$i]["type"];
				$TaobaokeItem[$i]["level"]= $Result_users[$i]["seller_credit"]["level"]; 
				$TaobaokeItem[$i]["state"]= $Result_users[$i]["location"]["state"]; 
				$TaobaokeItem[$i]["city"]= $Result_users[$i]["location"]["city"]; 
			}
		}
		
		$this->view->TaobaokeItem = $TaobaokeItem;
		$this->view->Result_users = $Result_users;
		
		//查找特定卖家--
		/*if($iid>0){
		    for($i=0;$i<PAGESIZE;$i++){
		        if(strip_tags($TaobaokeItem[$i]['title'])==$title && $TaobaokeItem[$i]['nick']==$sellernick){
			        $shoptype=$TaobaokeItem[$i]["type"];
				    $shoplevel=$TaobaokeItem[$i]["level"];
				    $click_url=$TaobaokeItem[$i]["click_url"];
				    $user_id=$TaobaokeItem[$i]["user_id"];
				    $fxje=ck_fxje($TaobaokeItem[$i]["commission"],$_SESSION['dduserlevel']);
				    $i=99;
			    }
		    }
		}*/
		
		//注意，$i应该是99，但是for判断时又增加了1
		/*if($iid>0 && $i!=100){
		    //获取掌柜基本信息
		    $Taoapi->method = 'taobao.user.get';
		    $Taoapi->fields = 'user_id,nick,consumer_protection,seller_credit,location,type';
		    $Taoapi->nick = $sellernick;
		    $TaoapiUsers = $Taoapi->Send('get','xml')->getArrayData();
		    $Result_users_iid = $TaoapiUsers["user"];
		    $user_id=$Result_users_iid["user_id"];
		    $shoptype=$Result_users_iid["type"];
		    $shoplevel=$Result_users_iid["seller_credit"]["level"];
		    $consumer_protection=$Result_users_iid["consumer_protection"];
		    $shopstate=$Result_users_iid["location"]["state"];
		    $shopcity=$Result_users_iid["location"]["city"];
			
			//获取该商品的返现金额
		    $Taoapi->method = 'taobao.taobaoke.items.convert';
		    $Taoapi->fields = 'commission';
		    $Taoapi->nick =  MYUSERNICK;
		    $Taoapi->num_iids=$iid;
		    $CommData = $Taoapi->Send('get','xml')->getArrayData();
		    if($CommData['taobaoke_items']['taobaoke_item']['commission']==''){
		        $CommData['taobaoke_items']['taobaoke_item']['commission']=get_fan($title,$sellernick,'commission');
		    }
		    $fxje=ck_fxje($CommData['taobaoke_items']['taobaoke_item']['commission'],$_SESSION["dduserlevel"]);
		}*/
	  	$Pagers = new Myutility_Pagination($total,$pagesize,0,0);	  
	  	$this->view->pagebar = $Pagers->getNavigation();
	}
	
	function topic() 
	{		
		$photoUrl = $this->getRequest()->getParam('pic');
		$layout = $this->_helper->layout();
    	$layout->disableLayout();
		header( "Location: ".base64_decode( $photoUrl ) );
		exit( );
	}
	
	function view() 
	{		
		$photoUrl = $this->getRequest()->getParam('pic');
		$layout = $this->_helper->layout();
	}


}

